<?php

namespace app\admin\controller;

use app\BaseController;

class Wechat extends BaseController
{

    /**
     * @param
     * @return 
     */
    public function encryptDecrypt()
    {
        $timeStamp      = "1409304348";
        $nonce          = "12351262347";
        $text           = "<xml>
                        <ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName>
                        <FromUserName><![CDATA[gh_7f083739789a]]></FromUserName>
                        <CreateTime>1407743423</CreateTime>
                        <MsgType><![CDATA[video]]></MsgType>
                        <Video>
                        <MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId>
                        <Title><![CDATA[testCallBackReplyVideo]]></Title>
                        <Description><![CDATA[testCallBackReplyVideo]]></Description>
                        </Video>
                        </xml>";

        $pc         = new \wxcrypt\WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
        $encryptMsg = '';
        $errCode    = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
        if ($errCode == 0) {
            print("加密后: " . $encryptMsg . "\n");
        } else {
            print($errCode . "\n");
        }

        exit;


        $xml_tree = new \DOMDocument();
        $xml_tree->loadXML($encryptMsg);
        $array_e = $xml_tree->getElementsByTagName('Encrypt');
        $array_s = $xml_tree->getElementsByTagName('MsgSignature');
        $encrypt = $array_e->item(0)->nodeValue;
        $msg_sign = $array_s->item(0)->nodeValue;

        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $encrypt);

        // 第三方收到公众号平台发送的消息
        $msg = '';
        $pc         = new \wxcrypt\WXBizMsgCrypt($token, $encodingAesKey, $appId);
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
        if ($errCode == 0) {
            print("解密后: " . $msg . "\n");
        } else {
            print($errCode . "\n");
        }
    }

    /**
     * 验证signature
     * https://figjam.gamer.gd/index.php
     * http://34660558.r12.cpolar.top/admin/Wechat/notifyInfo
     * @author  messha1r
     * @date    2025-09-04
     * @param        
     * @param        
     * @return string 
     */
    public function notifyInfo()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr = $_GET['echostr'];
        // $input = file_get_contents('php://input');
        // file_put_contents('./1.txt', json_encode($input) . "\n", FILE_APPEND);

        // file_put_contents('./1.txt', $signature . "\n", FILE_APPEND);
        // file_put_contents('./1.txt', $timestamp . "\n", FILE_APPEND);
        // file_put_contents('./1.txt', $nonce . "\n", FILE_APPEND);
        // file_put_contents('./1.txt', $echostr . "\n", FILE_APPEND);
        $token = $this->token;
        header('Content-Type: ' . 'text/plain');

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            // file_put_contents('./1.txt', 'here-ok' . "\n", FILE_APPEND);
            echo $echostr;
            exit;
        } else {
            // file_put_contents('./1.txt', 'here-failed' . "\n", FILE_APPEND);
            echo 'failed';
        }
    }
}
